package com.nuanshui.heatedloan.userinfo.service;

import com.google.common.collect.Lists;
import com.nuanshui.heatedloan.baseservice.service.CommonServiceImpl;
import com.nuanshui.framework.consts.TransactionManagerType;
import com.nuanshui.heatedloan.constant.ServiceConstant;
import com.nuanshui.heatedloan.dal.IdTypeHandler;
import com.nuanshui.heatedloan.userinfo.dal.mapper.UsersMapper;
import com.nuanshui.heatedloan.userinfo.facade.UserQueryService;
import com.nuanshui.heatedloan.userinfo.model.User;
import com.nuanshui.heatedloan.userinfo.model.UserInfo;
import com.nuanshui.heatedloan.userinfo.vo.UserVo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@Service(ServiceConstant.UserQueryService)
@Transactional(readOnly=true,value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
public class UserQueryServiceImpl extends CommonServiceImpl implements UserQueryService {

	@Autowired
	private UsersMapper userMapper;

	@Override
	public User load(String userId) {
		return userMapper.selectByPrimaryKey(userId);
	}

	@Override
	public User loadByLoginName(String loginname) {
		return userMapper.loadByLoginname(loginname);
	}

	@Override
	public User loadUserByUsername(String username) throws UsernameNotFoundException {
		UserInfo u = userMapper.loadByLoginname(username);
		if (u == null || !u.isEnabled()) return null;
		User user = new User();
		try {
			BeanUtils.copyProperties(user,u);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}

		return user;
	}

	@Override
	public User loadUserByPhone(String phone) {
		return userMapper.loadByPhone(phone);
	}

	@Override
	public UserInfo loadSimplerUserInfo(String userId) {
		return userMapper.loadSimplerUserInfo(userId);
	}

	@Override
	public UserInfo getUserInfo(String userId) {
		return userMapper.getUserInfo(userId);
	}

	@Override
	public UserInfo loadUserInfo(String userId) {
		return userMapper.loadUserInfo(userId);
	}

	@Override
	public List<UserVo> findListByName(String userName) {
		return userMapper.findByName(userName);
	}

	@Override
	public List<String> loadPhoneListByIds(List<String> userIds) {
		if(userIds == null || userIds.isEmpty()) return Lists.newArrayList();
		return userMapper.loadPhoneByIds(userIds);
	}

	@Override
	public List<String> loadAllUserIds() {
		List<Long> ids = userMapper.loadAllUserIds();
		List<String> results = Lists.newArrayList();
		for(Long id :ids){
			results.add(IdTypeHandler.encode(id));
		}
		return results;
	}

	@Override
	public List<User> loadAllUsers() {

		return userMapper.loadAllUsers();
	}

	@Override
	public List<User> loadUsers(List<Long> userIds) {
		if(userIds == null || userIds.isEmpty())
			return new ArrayList<User>();

		return userMapper.loadUsers(userIds);
	}

	@Override
	public List<UserInfo> loadUserInfos(List<Long> userIds) {
		if(userIds == null || userIds.isEmpty())
			return new ArrayList<UserInfo>();
		return userMapper.loadUserInfos(userIds);
	}

	@Override
	public List<User> loadUsers(String... userIds) {
		return userMapper.loadUsersByStringId(userIds);
	}

	@Override
	public List<User> loadUsersByPhone(List<String> phones) {
		if(phones == null || phones.isEmpty()) {
			return new ArrayList<User>();
		}
		return userMapper.loadUsersByPhone(phones);
	}

}
